package thread;

public class DeadLockDemo2 {
    //筷子
    public static Object chopsticks = new Object();
    //勺
    public static Object spoon = new Object();

    public static void main(String[] args) {
        //北方人
        Thread np = new Thread(){
            public void run(){
                System.out.println("北方人开始吃饭");
                System.out.println("北方人去拿筷子...");
                synchronized (chopsticks) {
                    System.out.println("北方人拿起筷子吃饭...");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("北方人吃完饭了");
                }
                System.out.println("北方人放下了筷子,去拿勺");
                synchronized (spoon){
                    System.out.println("北方人拿起了勺喝汤...");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("北方人喝完了汤");
                }
                System.out.println("北方人放下了勺");
                System.out.println("北方人吃饭完毕");
            }
        };

        Thread sp = new Thread(){
            public void run(){
                System.out.println("南方人开始吃饭");
                System.out.println("南方人去拿勺...");
                synchronized (spoon) {
                    System.out.println("南方人拿起勺喝汤...");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("南方人喝完汤了");
                }
                System.out.println("南方人放下勺,去拿筷子");
                synchronized (chopsticks){
                    System.out.println("南方人拿起了筷子吃饭...");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("南方人吃完饭了");
                }
                System.out.println("南方人放下了筷子");
                System.out.println("南方人吃饭完毕");
            }
        };
        np.start();
        sp.start();


    }
}
